<?php

/**
 * Description of PostAdmin
 *
 * @author Mahmoud
 */

namespace Kateba\AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Kateba\TTICOBundle\Entity\Post;

class PostAdmin extends Admin {

    /**
     * this variable holds the route name prefix for this actions
     * @var string 
     */
    protected $baseRouteName = 'post_admin';

    /**
     * this variable holds the url route prefix for this actions
     * @var string 
     */
    protected $baseRoutePattern = 'post';

    /**
     * this function configure the list action fields
     * @author Mahmoud
     * @param ListMapper $listMapper 
     */
    public function configureListFields(ListMapper $listMapper) {
        $listMapper
                ->addIdentifier('id')
                ->add('title')
                ->add('content')
                ->add('postType')
                ->add('createdAt')
                ->add('uploadedFile', NULL, array('template' => 'KatebaAdminBundle:Admin:list_file.html.twig'))
                ->add('image', NULL, array('template' => 'KatebaAdminBundle:Admin:list_image.html.twig'))
                ->add('_action', 'actions', array(
                    'actions' => array(
                        'view' => array(),
                        'edit' => array(),
                        'delete' => array(),
                    )
                ))
        ;
    }

    /**
     * this function configure the show action fields
     * @author Mahmoud
     * @param ShowMapper $showMapper 
     */
    public function configureShowField(ShowMapper $showMapper) {
        $showMapper
                ->add('id')
                ->add('title')
                ->add('content')
                ->add('postType')
                ->add('createdAt')
                ->add('uploadedFile', NULL, array('template' => 'KatebaAdminBundle:Admin:show_file.html.twig'))
                ->add('image', NULL, array('template' => 'KatebaAdminBundle:Admin:show_image.html.twig'))
        ;
    }

    /**
     * this function configure the list action filters fields
     * @author Mahmoud
     * @param DatagridMapper $datagridMapper 
     */
    public function configureDatagridFilters(DatagridMapper $datagridMapper) {
        $datagridMapper
                ->add('id')
                ->add('title')
                ->add('content')
                ->add('postType')
        ;
    }

    /**
     * this function configure the new, edit form fields
     * @author Mahmoud
     * @param FormMapper $formMapper 
     */
    public function configureFormFields(FormMapper $formMapper) {
        $post = new Post();
        $formMapper
                ->with('Post Fields')
                ->add('title')
                ->add('postType', 'choice', array('choices'   => $post->getPostTypes()))
                ->add('content', 'textarea', array('required' => false, 'attr' => array('class' => 'tinymce')))
                ->add('file', 'file', array('required' => false, 'label' => 'image'))
                ->add('document', 'file', array('required' => false, 'label' => 'document'))
                ->end()
        ;
    }

    /**
     * this function will be executed before the update of any old object
     * @author Mahmoud
     * @param \Kateba\TTICOBundle\Entity\Post $post
     */
    public function preUpdate($post) {
        //try to upload the image if exist
        $post->preUpload();
    }

    /**
     * this function will be executed after the update of any old object
     * @author Mahmoud
     * @param \Kateba\TTICOBundle\Entity\Post $post
     */
    public function postUpdate($post) {
        //upload the image
        $post->upload();
    }

    /**
     * this function add new validation to this entity
     * @author Mahmoud
     * @param \Sonata\AdminBundle\Validator\ErrorElement $errorElement
     * @param \Kateba\TTICOBundle\Entity\Post $object
     * @return void
     */
    public function validate(ErrorElement $errorElement, $object) {
        $errorElement
                ->with('content')
                ->assertNotNull()
                ->end()
        ;
    }

}

?>
